//
//  YCHUDView.m
//  YCAlertController
//
//  Created by Yochia on 2017/3/9.
//  Copyright © 2017年 Team Brutal. All rights reserved.
//

#import "YCHUDView.h"

@interface YCHUDView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIActivityIndicatorView * indicatorView;
@property (nonatomic, strong) NSMutableArray * constraints;

@end

@implementation YCHUDView

#pragma mark - Init

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10;
        self.clipsToBounds = YES;
        self.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return self;
}

#pragma mark - 构建视图

- (void)setTitle:(NSString *)title {
    _title = title;
    if (self.title && self.title.length > 0) {
        self.titleLabel.text = self.title;
    } else {
        [_titleLabel removeFromSuperview];
        _titleLabel = nil;
    }
    [self setNeedsUpdateConstraints];
}

- (void)setAwait:(BOOL)await {
    _await = await;
    if (_await) {
        [self.indicatorView startAnimating];
    } else {
        [_indicatorView removeFromSuperview];
        _indicatorView = nil;
    }
    [self setNeedsUpdateConstraints];
}

- (void)setMessage:(NSString *)message {
    _message = message;
    if (self.message && self.message.length > 0) {
        self.messageLabel.text = self.message;
    } else {
        [_messageLabel removeFromSuperview];
        _messageLabel = nil;
    }
    [self setNeedsUpdateConstraints];
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
        _titleLabel.font = [UIFont boldSystemFontOfSize:17];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIActivityIndicatorView *)indicatorView {
    if (!_indicatorView) {
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _indicatorView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_indicatorView];
    }
    return _indicatorView;
}

- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.textColor = [UIColor colorWithWhite:0.9 alpha:1];
        _messageLabel.font = [UIFont systemFontOfSize:15];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.numberOfLines = 0;
        _messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_messageLabel];
    }
    return _messageLabel;
}

#pragma mark - 更新约束

- (void)updateConstraints {
    [super updateConstraints];
    
    [self removeConstraints:self.constraints];
    self.constraints = [NSMutableArray array];
    
    if (_titleLabel) {
        NSArray * consts = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20@500)-[title]-(20@500)-|" options:0 metrics:nil views:@{@"title": _titleLabel}];
        [self.constraints addObjectsFromArray:consts];
        consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[title]" options:0 metrics:nil views:@{@"title": _titleLabel}];
        [self.constraints addObjectsFromArray:consts];
    }
    
    if (_indicatorView) {
        NSArray * consts = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10@300)-[indi]-(10@300)-|" options:0 metrics:nil views:@{@"indi": _indicatorView}];
        [self.constraints addObjectsFromArray:consts];
        [NSLayoutConstraint constraintWithItem:_indicatorView attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual toItem:self
                                     attribute:NSLayoutAttributeCenterX multiplier:1.0
                                      constant:0.0].active = YES;
        
        if (_titleLabel) {
            consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[title]-10-[indi]" options:0 metrics:nil
                                                               views:@{@"title": _titleLabel, @"indi": _indicatorView}];
            [self.constraints addObjectsFromArray:consts];
        } else {
            consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[indi]" options:0 metrics:nil
                                                               views:@{@"indi": _indicatorView}];
            [self.constraints addObjectsFromArray:consts];
        }
    }
    
    if (_messageLabel) {
        NSArray * consts = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20@800)-[messageLabel(<=240)]-(20@800)-|" options:0 metrics:nil views:@{@"messageLabel": _messageLabel}];
        [self.constraints addObjectsFromArray:consts];
        
        if (_indicatorView) {
            consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[indi]-10-[message(>=40)]" options:0 metrics:nil
                                                               views:@{@"indi": _indicatorView, @"message": _messageLabel}];
            [self.constraints addObjectsFromArray:consts];
        } else if (_titleLabel) {
            consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[title]-10-[message(>=40)]" options:0 metrics:nil
                                                               views:@{@"title": _titleLabel, @"message": _messageLabel}];
            [self.constraints addObjectsFromArray:consts];
        } else {
            consts = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[message(>=40)]" options:0 metrics:nil
                                                               views:@{@"message": _messageLabel}];
            [self.constraints addObjectsFromArray:consts];
        }
    }
    
    if (_messageLabel ?: _indicatorView ?: _titleLabel) {
        NSLayoutConstraint * bottomConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom
                                                                             relatedBy:NSLayoutRelationEqual toItem:_messageLabel ?: _indicatorView ?: _titleLabel
                                                                             attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10.0];
        bottomConstraint.priority = UILayoutPriorityDefaultLow;
        [self.constraints addObject:bottomConstraint];
    }
    
    [self addConstraints:self.constraints];
}

#pragma mark - 动画

- (void)popupViewAppearAnimationOrigin {
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
}

- (void)popupViewAppearAnimationDestination {
    self.transform = CGAffineTransformIdentity;
    self.alpha = 1;
}

- (void)popupViewDisappearAnimationOrigin {
    self.transform = CGAffineTransformIdentity;
    self.alpha = 1;
}

- (void)popupViewDisappearAnimationDestination {
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    self.alpha = 0;
}

@end
